package com.wsx.ones.akka.actor;

import akka.actor.ActorRef;
import akka.actor.Props;
import akka.actor.UntypedAbstractActor;
import com.wsx.ones.akka.domian.Data;
import com.wsx.ones.akka.domian.Message;
import com.wsx.ones.akka.service.TestService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import java.util.concurrent.CompletableFuture;

/**
 * @author wangshuaixin
 * @Title: com.wsx.ones.akka.actor.WorkActor
 * @Description: TODO
 * @date 2019/05/10
 */
@Component("workActor")
@Scope("prototype")
public class WorkActor extends UntypedAbstractActor {

    private final CompletableFuture<Message> future;

    public WorkActor(CompletableFuture<Message> future) {
        this.future = future;
    }

    @Autowired
    private TestService testService;



    @Override
    public void onReceive(Object message) throws Throwable {
        if (message instanceof Message) {
            Message m = (Message) message;

            m = testService.test(m);
            future.complete(m);
        } else if (message instanceof Data){
            ActorRef log = getContext().actorOf(Props.create(LogActor.class), "logActor");
            Data data = (Data) message;
            System.out.println("work actor :" + data.toString());
            log.tell(data, getSelf());
            future.complete(new Message().result("成功返回"));
        } else {
            unhandled(message);
        }

        getContext().stop(getSelf());
    }
}
